package com.huanglj.himalaya.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.huanglj.himalaya.activitys.PlayerActivity;
import com.huanglj.himalaya.adapters.DetailAdapter;
import com.huanglj.himalaya.base.BaseFragment;
import com.huanglj.himalaya.interfaces.IHistoryCallBack;
import com.huanglj.himalaya.presenters.HistoryPresenter;
import com.huanglj.himalaya.presenters.PlayerPresenter;
import com.huanglj.himalaya.utils.LogUtil;
import com.huanglj.himalaya.views.ComfirmDialog;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

public class HistoryFragment extends BaseFragment implements IHistoryCallBack {

    private static final String TAG = "HistoryFragment";
    private DetailAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private HistoryPresenter mHistoryPresenter;

    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {
        View view = layoutInflater.inflate(R.layout.fragment_history, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        LogUtil.d(TAG,"createHistoryFragment");
        mRecyclerView = view.findViewById(R.id.history_data_item);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        mAdapter = new DetailAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mHistoryPresenter = HistoryPresenter.getInstance();
        mHistoryPresenter.registerViewCallBack(this);
        mHistoryPresenter.getHistoryList();
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.bottom= UIUtil.dip2px(view.getContext(),1);
                outRect.top = UIUtil.dip2px(view.getContext(),1);
            }
        });
        mAdapter.setOnDetailOnclickListener(new DetailAdapter.OnDetailOnclickListener() {
            @Override
            public void onItemClick(List<Track> trackList, int position) {
                PlayerPresenter.getPlayerPresenter().setPlayList(trackList,position);
                Intent intent = new Intent(getContext(), PlayerActivity.class);
                startActivity(intent);
            }
        });
        mAdapter.setOnLongClickListener(new DetailAdapter.OnLongClickListener() {
            String[] dialogText = getContext().getResources().getStringArray(R.array.dialog_history);
            @Override
            public void onLongItemClick(final Track track) {
                ComfirmDialog dialog = new ComfirmDialog(getActivity());
                dialog.show();
                dialog.setText(dialogText);
                dialog.setOnDialogClickListener(new ComfirmDialog.OnDialogClickListener() {
                    @Override
                    public void cancelClick() {
                        mHistoryPresenter.deleteHistory(track);
                    }

                    @Override
                    public void giveUpClick() {

                    }
                });
            }
        });
    }

    @Override
    public void onHistoryAdd(boolean success) {

    }

    @Override
    public void onHistoryDelete(boolean success) {

    }

    @Override
    public void onHistoryClear() {

    }

    @Override
    public void onHistoryList(List<Track> trackList) {
        LogUtil.d(TAG,"trackList.size: "+trackList.size());
        if (mAdapter != null) {
            mAdapter.setData(trackList);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHistoryPresenter.unRegisterViewCallBack(this);
    }
}
